﻿/**
 * This is a part of MCServe, which is available under the Apache license 2.0.
 * With this code, you may do whatever you want as long as that is not in conflict with this license.
 * In addition, you should have a reference to the original project to include (http://mcserve.codeplex.com).
 * 
 * Author       : Lars Krämer ( Xe77a )
 * Website      : http://www.lkraemer.de
 * Project      : MCServe (http://mcserve.codeplex.com)
 * Copyright    : Copyright (c) 2013 lkraemer.de
 * License      : http://mcserve.codeplex.com/license
 * Description  : This is a part of the core functionality of the server
 */

using MCServeCore.PlayerObjects;
using MCServeLib.Types;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;

namespace MCServeCore.Components
{
    public class ServerOpsList
    {
        private ObservableCollection<Player> _Players = new ObservableCollection<Player>();
        public ObservableCollection<Player> Players
        {
            get
            {
                return _Players;
            }
        }

        public ServerOpsList()
        {
            Load();
        }

        public void Add(Player player)
        {
            _Players.Add(player);
            Save();
        }
        public void Add(String player)
        {
            Player pl = new Player();
            pl.Name = player;
            _Players.Add(pl);
            Save();
        }

        public void Remove(String name)
        {
            for (int i = _Players.Count - 1; i >= 0; i--)
            {
                if (_Players[i].Name == name)
                {
                    _Players.RemoveAt(i);
                    Save();
                }
            }
        }

        private Boolean Load()
        {
            if (File.Exists(GameServer.Paths.Get(MCServePaths.LIST_OPS)))
            {
                StreamReader ConfigReader = new StreamReader(GameServer.Paths.Get(MCServePaths.LIST_OPS));

                List<String> List = new List<String>();
                List = JsonConvert.DeserializeObject<List<String>>(ConfigReader.ReadToEnd());

                if (List == null)
                {
                    return true;
                }

                foreach (String PlayerName in List)
                {
                    Add(PlayerName);
                }

                ConfigReader.Close();

                return true;
            }
            else
            {
                return false;
            }
        }

        private Boolean Save()
        {
            if (GameServer.Paths.Get(MCServePaths.LIST_OPS) == null)
            {
                return false;
            }

            try
            {
                File.Delete(GameServer.Paths.Get(MCServePaths.LIST_OPS));

                List<String> List = new List<String>();

                foreach (Player pl in Players)
                {
                    List.Add(pl.Name);
                }

                StreamWriter ConfigWriter = new StreamWriter(GameServer.Paths.Get(MCServePaths.LIST_OPS));
                ConfigWriter.Write(JsonConvert.SerializeObject(List));
                ConfigWriter.Close();
                return true;
            } catch (Exception err)
            {
                GameServer.Console.SendMessage(err.Message, ServerConsole.OutputTarget.ErrorLogAndConsole, ServerConsole.OutputTypes.Error);
                return false;
            }
        }
    }
}
